package com.yadang.homework;

import java.util.*;

/**
 * @author: 亚当
 * @date: 2023年02月05日 20:37
 * @package: com.yadang.homework
 * @Description: 将map集合中相同value的key取出来
 */
public class FromValueToKeyUtil {
    public static List<String>  getValue(HashMap<Integer, Object> map){
        String resultStr = "";
        LinkedList<String> resultList = new LinkedList<>();
        //建立一个数组用来存储map的数值
        List<String> list = new ArrayList<String>();
        //遍历map的值，然后赋值到list
        Collection values = map.values();
        for(Iterator iterator1 = values.iterator();iterator1.hasNext();){
            list.add((String) iterator1.next());
        }
        //找到list中相等的字符串
        String str = null;
        for(int i=0;i<list.size()-1;i++){
            for(int j=i+1;j<list.size();j++){
                if(list.get(i)==list.get(j)){
                    System.out.println(list.get(i));
                    str = list.get(i);
                }
            }
        }
        //输出等于strl值的key值
        Collection col1 = map.entrySet();
        String str2 = null;
        if (str != null){
            for(Iterator ite2 = col1.iterator();ite2.hasNext();){
                Object obj = ite2.next();
                str2 = obj.toString();
                if(str.equals(str2.substring(str2.indexOf("=")+1,str2.length()))){
                    String substring = str2.substring(0, str2.lastIndexOf("="));
                    //System.out.println(str2.substring(0,str2.lastIndexOf("=")));
                    //resultStr = resultStr + str2.substring(0, str2.lastIndexOf("="));
                    resultList.add(substring);
                }
            }
        }
        return resultList;
    }
}
